package com.joplus.housekeeper.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean;
import com.joplus.housekeeper.utils.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/6/26.
 */
public class EventSelectHorizontalAdapter extends RecyclerView.Adapter<EventSelectHorizontalAdapter.HorizontalItemHolder> {



    List<EventPayDateilSessionDTOsBean> datas;

    AdapterView.OnItemClickListener OnItemClickListener;
    int mposition=0;





    public EventSelectHorizontalAdapter(List<EventPayDateilSessionDTOsBean> datas) {

    this.datas=datas;
    }
    public void notifyData( List<EventPayDateilSessionDTOsBean> datas,int position){
        this.datas=datas;
        notifyDataSetChanged();
        mposition=position;
    }
    public void selectPosition(int position){
        mposition=position;

    }






    @Override
    public HorizontalItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.item_event_select_horizontal, parent, false);

        return new HorizontalItemHolder(view, this);
    }

    @Override
    public void onBindViewHolder(HorizontalItemHolder holder, int position) {
        EventPayDateilSessionDTOsBean bean = datas.get(position);
        StringUtils.setText(holder.tv_firstlabe,bean.getFirstLabelContent());
        StringUtils.setText(holder.tv_secondlable,bean.getSecondLabelContent());
        if(position==mposition){
            holder.layout.setBackgroundResource(R.drawable.item_event_select_background);
        }else{
            holder.layout.setBackgroundResource(R.drawable.gray_empty_round_rectangle);
        }


    }

    @Override
    public int getItemCount() {

        if (datas == null) {
            return 0;
        } else {
            return datas.size();
        }

    }


    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        OnItemClickListener = listener;
    }

    private void onItemHolderClick(HorizontalItemHolder holder) {
        if (OnItemClickListener != null) {
            OnItemClickListener.onItemClick(null, holder.itemView, holder.getAdapterPosition(), holder.getItemId());
        }
    }

    public static class HorizontalItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tv_firstlabe;
        TextView tv_secondlable;
        LinearLayout layout;

        EventSelectHorizontalAdapter adapter;


        public HorizontalItemHolder(View itemView, EventSelectHorizontalAdapter adapter) {
            super(itemView);

            layout= (LinearLayout) itemView.findViewById(R.id.layout_shop_list);
            tv_firstlabe = (TextView) itemView.findViewById(R.id.tv_firstlabe);
            tv_secondlable = (TextView) itemView.findViewById(R.id.tv_secondlable);
            this.adapter = adapter;
            layout.setOnClickListener(this);



        }

        @Override
        public void onClick(View v) {
            adapter.onItemHolderClick(this);
        }
    }


}
